package skitur.client;

import java.util.Date;
import java.util.Map.Entry;

import skitur.shared.Commons;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DatePicker;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Skitur implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final SkiturServiceAsync greetingService = GWT
			.create(SkiturService.class);
	
	// parse the value to int

//	public static native String getParam(String msg)
//	/*-{
//	 return com.google.gwt.user.client.Window.Location.getParameter(msg);
//	}-*/;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Button sendButton = new Button("Add report");
		RootPanel.get("sendButtonContainer").add(sendButton);
		final Button cancelButton = new Button("Cancel");
		RootPanel.get("cancelContainer").add(cancelButton);
		final TextBox user = new TextBox();
		RootPanel.get("user").add(user);
		//final ListBox region = new ListBox();
		//RootPanel.get("region").add(region);
		final ListBox overallConditions = new ListBox();
		RootPanel.get("overallConditions").add(overallConditions);
		final ListBox snowType = new ListBox();
		RootPanel.get("snowType").add(snowType);
		final ListBox avalancheRisk = new ListBox();
		RootPanel.get("avalancheRisk").add(avalancheRisk);
		final TextArea comment = new TextArea();
		RootPanel.get("comment").add(comment);
		final DatePicker date = new DatePicker(); 
		RootPanel.get("tripDate").add(date);
		date.setValue(new Date());
		
		
		greetingService.getUser(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				
				
			}

			@Override
			public void onSuccess(String result) {
				user.setText(result);
				
			}
		});


		for(Entry<String, Integer> p : Commons.conditions.entrySet()){
			overallConditions.addItem(p.getKey());
		}
		snowType.addItem("puch");
		snowType.addItem("firn");
		snowType.addItem("wet");
		snowType.addItem("crust");
		
//		for(String r : Commons.regions){
//			region.addItem(r);
//		}
		
		//string value = com.google.gwt.user.client.Window.Location.getParameter("region");
		//region.addItem(com.google.gwt.user.client.Window.Location.getParameter("region"));
				//getParam("region"));
		
		avalancheRisk.addItem("0");
		avalancheRisk.addItem("I");
		avalancheRisk.addItem("II");
		avalancheRisk.addItem("III");
		avalancheRisk.addItem("IV");
		avalancheRisk.addItem("V");
		
		
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		
	
		

		// Focus the cursor on the name field when the app loads
		user.setFocus(true);
		user.selectAll();

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer() {
				if (//(region.getSelectedIndex() != -1)&& 
						(overallConditions.getSelectedIndex() != -1)) {
					sendButton.setEnabled(false);
					greetingService.addReport(date.getValue(),
							com.google.gwt.user.client.Window.Location.getParameter("region"),
							//region.getItemText(region.getSelectedIndex()),
							user.getText(), 
							Commons.conditions.get(overallConditions.getValue(overallConditions.getSelectedIndex())),
							snowType.getItemText(snowType.getSelectedIndex()), 
							avalancheRisk.getSelectedIndex(), comment.getText(),
							new AsyncCallback<String>() {
								public void onFailure(Throwable caught) {
									sendButton.setEnabled(true);
									Window.Location.assign("/index.jsp");
								}

								public void onSuccess(String result) {
									sendButton.setEnabled(true);
									Window.Location.assign("/index.jsp");
								}
							});
				}
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		cancelButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				Window.Location.assign("/index.jsp");
			}
			
		});
	//	nameField.addKeyUpHandler(handler);
	}
}
